﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using TongHopBookFlightDAO;
using TongHopBookFlightDAO.DAO;

namespace TongHopBookFlightCores.Cores
{
    public class RutTrichTrangVietNamAirlines
    {
        private HtmlDocument document;
        private static string host = "";

        public void TimChuyenBayTheoMotChieu(string diemDi, string diemDen, DateTime ngaydi)
        {
            int flightcountout = 0;
            long GiaVe = 0;
            string chongoi = "";

            HtmlWeb web = new HtmlWeb();
            MakeUrl(diemDi, diemDen, ngaydi.Day, ngaydi.Month);
            document = web.Load(host);

            HtmlNodeCollection lsdiadiem = document.DocumentNode.SelectNodes("//*[@id='departCity']");
            HtmlNodeCollection depTimeOut = document.DocumentNode.SelectNodes("//table[@id='bfm_tbl_out']/tbody");

 //           HtmlNodeCollection TimeArrOut = document.DocumentNode.SelectNodes("//table[@id='bfm_tbl_out']//div[@class='arrTimeSR']/p[@class='highlight']");

            if (depTimeOut != null)
            {
                flightcountout = depTimeOut.Count;
            }
           
            for (int i = 0; i < flightcountout; i++)
            {
                ChuyenBay chuyenBay = new ChuyenBay();

                HtmlNodeCollection moreraces = document.DocumentNode.SelectNodes("//table[@id ='bfm_tbl_out']//tr[@id='row_out_" + i + "']//td[@class='matrix_cal_body matrix_cal_body_arr']/div");
                if (moreraces.Count <= 1)
                {
                    HtmlNode depTime = document.DocumentNode.SelectSingleNode("//table[@id ='bfm_tbl_out']//tr[@id='row_out_" + i + "']//div[@class='depTimeSR']//p[@class='highlight']");
                    HtmlNode TimeArr = document.DocumentNode.SelectSingleNode("//table[@id ='bfm_tbl_out']//tr[@id='row_out_" + i + "']//div[@class='arrTimeSR']/p[@class='highlight']");
                    HtmlNode codeFlight = document.DocumentNode.SelectSingleNode("//table[@id ='bfm_tbl_out']//tr[@id='row_out_" + i + "']//div[@class = 'flightInfoDiv']//span[@class='matrix_flight_num']//span[@class='matrix_airline_code']");

                    string codeflightregular = Replacestring(codeFlight.InnerText, @"&nbsp;|\s|\n");
                    HtmlNode codeairflight = document.DocumentNode.SelectSingleNode("//table[@id ='bfm_tbl_out']//tr[@id='row_out_" + i + "']//div[@class = 'flightInfoDiv']//span[@class='matrix_flight_equip']");
                    HtmlNode stop = document.DocumentNode.SelectSingleNode("//table[@id ='bfm_tbl_out']//tr[@id='row_out_" + i + "']//div[@class ='destStops']");
                    HtmlNode dunga = null;
                    if (stop == null)
                        dunga = document.DocumentNode.SelectSingleNode("//table[@id ='bfm_tbl_out']//tr[@id='row_out_" + i + "']//td[@class='matrix_cal_body matrix_cal_body_stops']//a");


                    chuyenBay.HangHangKhongID = HangHangKhongDAO.SelectByName(Constants.VIETNAMAIRLINES_NAME).HangHangKhongID;
                    chuyenBay.DiemDiID = DiaDiemDAO.SelectByCode(diemDi).DiaDiemID;
                    chuyenBay.DiemDenID = DiaDiemDAO.SelectByCode(diemDen).DiaDiemID;
                    chuyenBay.MaChuyenBay = codeflightregular;
                    chuyenBay.NgayDi = ngaydi;
                 

                    //Insert data for flight
                    if (stop == null)
                        chuyenBay.DiemDung = Replacestring(dunga.InnerText, @"&nbsp;|\t|\n");
                    else
                        chuyenBay.DiemDung = stop.InnerText;

                    int chuyenBayID = ChuyenBayDAO.Insert(chuyenBay);

                    if (chuyenBayID > 0)
                    {
                        //Insert data detail for flight

                        ChiTietChuyenBay chiTietChuyenBay = new ChiTietChuyenBay();
                        chiTietChuyenBay.ThoiGianDi = depTime.InnerText;
                        chiTietChuyenBay.ThoiGianDen = TimeArr.InnerText;
                        chiTietChuyenBay.ChuyenBayID = chuyenBayID;
                        chiTietChuyenBay.DiemDiID = chuyenBay.DiemDiID;
                        chiTietChuyenBay.DiemDenID = chuyenBay.DiemDenID;

                        ChiTietChuyenBayDAO.Insert(chiTietChuyenBay);

                        //Insert data GiaVe
                        int k = 1;
                        for (int j = 0; j < 4; j++)
                        {
                            HtmlNode fare = document.DocumentNode.SelectSingleNode("//table[@id ='bfm_tbl_out']//tr[@id='row_out_" + i + "']//td[@id='matrix_cal_body_fare" + (j + k) + "_" + i + "_out']//span[@class='step2PricePoints ']");
                            if (fare != null)
                            {
                                GiaVe = long.Parse(Replacestring(fare.InnerText, @"\s|\n|VND|\."));
                                HtmlNode text = document.DocumentNode.SelectSingleNode("//table[@id ='bfm_tbl_out']//tr[@id='row_out_" + i + "']//td[@id='matrix_cal_body_fare" + (j + k) + "_" + i + "_out']//div[@class='matrix_cal_seatsRemaining_text1']");
                                if (text != null)
                                    chongoi = Replacestring(text.InnerText, @"  |\n|\t");
                            }
                            InsertDataGiaVe(j + 1, chuyenBayID, GiaVe, chongoi);
                            k++;
                            GiaVe = 0;
                            chongoi = "";
                        }
                    }
                }
                else
                {
                    chuyenBay.HangHangKhongID = HangHangKhongDAO.SelectByName(Constants.VIETNAMAIRLINES_NAME).HangHangKhongID;
                    chuyenBay.DiemDiID = DiaDiemDAO.SelectByCode(diemDi).DiaDiemID;
                    chuyenBay.DiemDenID = DiaDiemDAO.SelectByCode(diemDen).DiaDiemID;
                    chuyenBay.MaChuyenBay = "";
                    chuyenBay.NgayDi = ngaydi;
                    chuyenBay.DiemDung = "Bay thẳng";

                    int chuyenBayID = ChuyenBayDAO.Insert(chuyenBay);

                    for (int h = 0; h < moreraces.Count; h++)
                    {
                        HtmlNode depTime = document.DocumentNode.SelectSingleNode("//table[@id ='bfm_tbl_out']//tr[@id='row_out_" + i + "']//td[2]//div[" + (h + 1) + "]//p[@class='highlight']");
                        HtmlNode depDiemDi = document.DocumentNode.SelectSingleNode("//table[@id ='bfm_tbl_out']//tr[@id='row_out_" + i + "']//td[2]//div[" + (h + 1) + "]//a");
                        HtmlNode TimeArr = document.DocumentNode.SelectSingleNode("//table[@id ='bfm_tbl_out']//tr[@id='row_out_" + i + "']//td[@class='matrix_cal_body matrix_cal_body_arr']//div[" + (h + 1) + "]//p[@class='highlight']");
                        HtmlNode ArrDiemDen = document.DocumentNode.SelectSingleNode("//table[@id ='bfm_tbl_out']//tr[@id='row_out_" + i + "']//td[@class='matrix_cal_body matrix_cal_body_arr']//div[" + (h + 1) + "]//a");

                        HtmlNode codeaircraft = document.DocumentNode.SelectSingleNode("//table[@id ='bfm_tbl_out']//tr[@id='row_out_" + i + "']//td[@class='matrix_cal_body matrix_cal_body_flifo']//div[" + (h + 1) + "]//span[@class='matrix_flight_num']//span[@class='matrix_airline_code']");
                        
                        if (chuyenBayID > 0)
                        {
                            //Insert data detail for flight
                            ChiTietChuyenBay chiTietChuyenBay = new ChiTietChuyenBay();
                            chiTietChuyenBay.ThoiGianDi = depTime.InnerText;
                            chiTietChuyenBay.ThoiGianDen = TimeArr.InnerText;
                            chiTietChuyenBay.ChuyenBayID = chuyenBayID;
                            chiTietChuyenBay.DiemDiID = DiaDiemDAO.SelectByCode(depDiemDi.InnerText).DiaDiemID;
                            chiTietChuyenBay.DiemDenID = DiaDiemDAO.SelectByCode(ArrDiemDen.InnerText).DiaDiemID;

                            ChiTietChuyenBayDAO.Insert(chiTietChuyenBay);

                        }
                    }
                    int k = 1;
                    for (int j = 0; j < 4; j++)
                    {
                        HtmlNode fare = document.DocumentNode.SelectSingleNode("//table[@id ='bfm_tbl_out']//tr[@id='row_out_" + i + "']//td[@id='matrix_cal_body_fare" + (j + k) + "_" + i + "_out']//span[@class='step2PricePoints ']");
                        if (fare != null)
                        {
                            GiaVe = long.Parse(Replacestring(fare.InnerText, @"\s|\n|VND|\."));
                            HtmlNode text = document.DocumentNode.SelectSingleNode("//table[@id ='bfm_tbl_out']//tr[@id='row_out_" + i + "']//td[@id='matrix_cal_body_fare" + (j + k) + "_" + i + "_out']//div[@class='matrix_cal_seatsRemaining_text1']");
                            if (text != null)
                                chongoi = Replacestring(text.InnerText, @"  |\n|\t");
                        }
                        InsertDataGiaVe(j + 1, chuyenBayID, GiaVe, chongoi);
                        k++;
                        GiaVe = 0;
                        chongoi = "";
                    }
                }
            }
        }

        public void MakeUrl(string diemDi, string diemDen, int ngaydi, int thangdi)
        {
            string mathangdi = GetMaThang(thangdi);
            string mathangve = "";
            int ngayve;
            if (ngaydi + 3 > 31)
                mathangve = GetMaThang(thangdi + 1);
            else
                mathangve = GetMaThang(thangdi);
            ngayve = ngaydi + 3;
            if (ngaydi + 3 == 32)
                ngayve = 1;
            if (ngaydi + 3 == 33)
                ngayve = 2;
            if (ngaydi + 3 == 34)
                ngayve = 3;


            host = "https://cat.sabresonicweb.com/SSWVN/meridia?language=vi&posid=B3QE&page=requestAirMessage_air&action=airRequest&realrequestAir=realRequestAir&actionType=nonFlex&classService=CoachClass&currency=VND&depTime=0600&retTime=0600&direction=onewaytravel" +
                "&departCity=" + diemDi
                + "&depDay=" + ngaydi
                + "&depMonth=" + mathangdi
                + "&temp_date=&"
                + "returnCity=" + diemDen
                + "&retDay=" + ngayve
                + "&retMonth=" + mathangve
                + "&temp_date="
                + "&ADT=1&CHD=0&INF=0&submit=T%C3%ACm%20chuy%E1%BA%BFn%20bay&wpf_timed_action_0VNAirline_00215IBEFastTabV2_00215bookingYourTrip_00515vn_0051513b91d62f4b_00515340af_1=&ShowNote=Qu%C3%BD%20kh%C3%A1ch%20c%E1%BA%A7%20nh%E1%BA%AD%20%C4%91u7847%20y%20%C4%91u7911%20%20th%C3%B4ng%20tin%20%C4%91u7875%20%20l%C3%A0m%20th%E1%BB%A7t%E1%BB%A5%20!&wclang=VI&txtFamilyName=&txtMidName=&slDeparture=&txtConfirmCode=&txtFlightNumber=&rdoDay=on&";
        }

        public void InsertDataGiaVe(int LoaiGiaVeID, int ChuyenBayID, long giave, string ChoNgoi)
        {
            GiaVe giaVe = new GiaVe();
            giaVe.LoaiGiaVeID = LoaiGiaVeID;
            giaVe.ChuyenBayID = ChuyenBayID;
            giaVe.ChoNgoi = ChoNgoi;
            giaVe.Gia = giave;

            GiaVeDAO.Insert(giaVe);
        }

        public string Replacestring(string a, string pat)
        {
            string pattern = pat;
            Regex myregex = new Regex(pattern);
            string ketqua = myregex.Replace(a, "");

            return ketqua;
        }

        public string GetMaThang(int thang)
        {
            if (thang == 1)
                return "JAN";
            if (thang == 2)
                return "FEB";
            if (thang == 3)
                return "MAR";
            if (thang == 4)
                return "APR";
            if (thang == 5)
                return "MAY";
            if (thang == 6)
                return "JUN";
            if (thang == 7)
                return "JUL";
            if (thang == 8)
                return "AUG";
            if (thang == 9)
                return "SEP";
            if (thang == 10)
                return "OCT";
            if (thang == 11)
                return "NOV";
            if (thang == 12)
                return "DEC";
            return null;

        }
    }
}
